import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Test0110
 *
 * @author byc
 * @date 2022-01-10
 */
public class Test0110 {
    public static void main(String[] args) {

        //Test 1
        List<Integer> list1 = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
        // 遍历输出符合条件的元素
//        list1.stream().filter(x -> x > 6).forEach(System.out::println);
        // 匹配第一个
        Optional<Integer> findFirst = list1.stream().filter(x -> x > 6).findFirst();
        // 匹配任意（适用于并行流）
        Optional<Integer> findAny = list1.parallelStream().filter(x -> x > 6).findAny();
        // 是否包含符合特定条件的元素
        boolean anyMatch = list1.stream().anyMatch(x -> x > 6);
        System.out.println("匹配第一个值：" + findFirst.get());
        System.out.println("匹配任意一个值：" + findAny.get());
        System.out.println("是否存在大于6的值：" + anyMatch);

        System.out.println("--------------------------");

        //Test 2
        List<String> list2 = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");

        Optional<String> max = list2.stream().max(Comparator.comparing(String::length));
        System.out.println("最长的字符串：" + max.get());

        System.out.println("--------------------------");

        //Test 3
        List<Integer> list3 = Arrays.asList(7, 6, 9, 4, 11, 6);
        // 自然排序
        Optional<Integer> max1 = list3.stream().max(Integer::compareTo);
        // 自定义排序
        Optional<Integer> max2 = list3.stream().max(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println("自然排序的最大值：" + max1.get());
        System.out.println("自定义排序的最大值：" + max2.get());

        System.out.println("---------------------------");

        //Test 4
        String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
        List<String> strList = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());

        List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);
        List<Integer> intListNew = intList.stream().map(x -> x + 3).collect(Collectors.toList());

        System.out.println("每个元素大写：" + strList);
        System.out.println("每个元素+3：" + intListNew);

        System.out.println("---------------------------");

        //Test5 练个字符串数组 合并成一个新的字符串
        List<String> strList1 = Arrays.asList("卢,本,伟,你,在","打,你,吗,呢");
        List<String> strListNew = strList1.stream().flatMap(s -> {
            String[] split = s.split(",");
            Stream<String> s2 = Arrays.stream(split);
            return s2;
        }).collect(Collectors.toList());
        System.out.println(strList1);
        System.out.println(strListNew);



        System.out.println("---------------------------");

        //reduce缩减
        List<Integer> integerList = Arrays.asList(1,3,5,7,9,13,53);
        //求和
        Optional<Integer> sum1 = integerList.stream().reduce((x,y) -> x + y);
        Optional<Integer> sum2 = integerList.stream().reduce(Integer::sum);
        Integer sum3 = integerList.stream().reduce(0,Integer::sum);
        //求乘积
        Optional<Integer> product = integerList.stream().reduce((x,y) -> x * y);
        //求最大值
        Optional<Integer> max51 = integerList.stream().reduce((x,y) -> x > y ? x : y);
        Integer max52 = integerList.stream().reduce(1,Integer::max);

        System.out.println(sum1.get());
        System.out.println(product.get());
        System.out.println(max51.get());

    }
}
